﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.NodeFactory;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
using umbraco;


namespace SPCFMedicineInformer.Masterpages
{
    public partial class Content_ConditionDetail : System.Web.UI.MasterPage
    {
        public IEnumerable<Node> ConditionSymptoms { get; set; }
        public string ChosenSymptoms { get; set; }
        public string ConditionName { get; set; }
        string CondName { get; set; }
        int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int tempInt;
                string strId = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
                ChosenSymptoms = Request.QueryString["cs"] != null ? Request.QueryString["cs"].ToString() : "";
                id = int.TryParse(strId,out tempInt) ? tempInt : 0;
                Node node = id > 0 ? new Node(id) : null;
                if (node != null && !string.IsNullOrEmpty(node.Name) && node.NodeTypeAlias == "Condition")
                {
                    var Condition = ContentHelper.GetByNodeId<Condition>(node.Id);
                    var medIds = Condition.MedicineTag.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    var sympIds = Condition.SymptomTag.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    ConditionName = Condition.Name;
                    BindSymptoms(sympIds);
                    BindMedicine(medIds);
                    litDescription.Text = Condition.Description;
                    string imgUrl = !string.IsNullOrEmpty(Condition.ImageUrl) ? Condition.ImageUrl : "/images/web-blue/default_condition.png";
                    string file = imgUrl.Substring(imgUrl.LastIndexOf('/') + 1);
                    img.ImageUrl = imgUrl;
                    img.AlternateText = file;
                    img.ToolTip = file;
                    linkImg.NavigateUrl = imgUrl;
                    linkImg.ToolTip = Condition.ConditionName;
                    ShowContent(true);
                }
            }
        }
        void ShowContent(bool b)
        {
            plcContent.Visible = b;
            plcError.Visible = !b;
        }
        void BindMedicine(List<string> medids)
        {
            List<MedicineItem> list = new List<MedicineItem>();
            int tempInt;
            string strIds = Request.QueryString["am"] != null ? Request.QueryString["am"].ToString() : "";
            int age = Request.QueryString["age"] != null ? int.TryParse(Request.QueryString["age"].ToString(),out tempInt) ? tempInt : -1 : -1;
            var allergicList = strIds.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in medids)
            {
                int id = int.TryParse(item, out tempInt) ? tempInt : 0;
                if (id == 0 || allergicList.Contains(id.ToString())) continue;
                var node = ContentHelper.GetByNodeId<MedicineItem>(id);
                if (node != null) 
                    list.Add(ContentHelper.GetByNodeId<MedicineItem>(id));
            }
            list = list.Where(node =>  age == -1 || 
                                (
                                    (node.PrescriptionFromAge <= age && node.PrescriptionToAge >= age) || 
                                    node.PrescriptionAgeType == "All Ages" ||
                                    node.PrescriptionAgeType == "" ||
                                    node.PrescriptionAgeType == null
                                )
                                ).ToList();
            RepMedicineItem.DataSource = list;
            RepMedicineItem.DataBind();
        }
        void BindSymptoms(List<string> sympIds)
        {
            var csv = string.Join(",", sympIds);
            ConditionSymptoms = uQuery.GetNodesByCsv(csv);
        }
    }
}